package com.bbcare.img.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtil;
import com.bbcare.img.plat.service.IEquipManageService;

/**
 * @name: 设备管理控制类
 * @author: DemonLee
 * @createTime: 2017.10.26
 * @description: 设备添加、修改及查询等操作
 * @modify:
 *
 */
@Controller
@RequestMapping("/setting/equipManage")
public class EquipManageController {
    private final Log logger = LogFactory.getLog(EquipManageController.class);

    @Autowired
    private IEquipManageService equipManageService;

    // 查询当前租户下的设备分类
    @ResponseBody
    @RequestMapping("/queryEquipClass")
    public ResultModel<Object> qryPatientByPhone(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in queryEquipClass----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtil.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);
        List<Object> list = equipManageService.listEquipClass(mapIn);

        Map<String, Object> retMap = new HashMap<String, Object>();
        int total = 0;
        if (null != list) {
            total = list.size();
        }
        retMap.put("rows", list);
        retMap.put("total", total);

        return new ResultModel<Object>(retMap);
    }

    // 新增设备
    @ResponseBody
    @RequestMapping("/addNewEquip")
    public ResultModel<Object> addNewEquip(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in addNewEquip----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 操作信息校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        Map<String, Object> chkInfo = ToolUtil.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        chkVar.put("opNote", "操作备注");
        ToolUtil.checkInputInfo("oprInfo", chkInfo, chkVar);
        chkInfo.put("updateTime", DateUtil.getSysDateA());
        chkInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));
        // 设备信息校验
        chkVar.clear();
        chkInfo = ToolUtil.typeCast(mapIn.get("equipInfo"));
        chkVar.put("classId", "设备类型id");
        chkVar.put("equipAlias", "设备编号");
        chkVar.put("programId", "小程序编号");
        ToolUtil.checkInputInfo("equipInfo", chkInfo, chkVar);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtil.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);
        Map<String, Object> retMap = equipManageService.saveNewEquip(mapIn);

        return new ResultModel<Object>(retMap);
    }

    // 修改设备
    @ResponseBody
    @RequestMapping("/modifyEquip")
    public ResultModel<Object> modifyEquip(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in modifyEquip----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 操作信息校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        Map<String, Object> chkInfo = ToolUtil.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        chkVar.put("opNote", "操作备注");
        ToolUtil.checkInputInfo("oprInfo", chkInfo, chkVar);
        chkInfo.put("updateTime", DateUtil.getSysDateA());
        chkInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));
        // 设备信息校验
        chkVar.clear();
        chkInfo = ToolUtil.typeCast(mapIn.get("equipInfo"));
        chkVar.put("classId", "设备类型id");
        chkVar.put("equipAlias", "设备编号");
        chkVar.put("programId", "小程序编号");
        chkVar.put("equipId", "设备id");
        ToolUtil.checkInputInfo("equipInfo", chkInfo, chkVar);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtil.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);
        Map<String, Object> retMap = equipManageService.updateEquipInfo(mapIn);

        return new ResultModel<Object>(retMap);
    }

    // 设置设备为无效或有效
    @ResponseBody
    @RequestMapping("/setEquipStatus")
    public ResultModel<Object> setEquipStatus(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in setEquipStatus----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 操作信息校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        Map<String, Object> chkInfo = ToolUtil.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        chkVar.put("opNote", "操作备注");
        ToolUtil.checkInputInfo("oprInfo", chkInfo, chkVar);
        chkInfo.put("updateTime", DateUtil.getSysDateA());
        chkInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));
        // 设备信息校验
        chkVar.clear();
        chkInfo = ToolUtil.typeCast(mapIn.get("equipInfo"));
        chkVar.put("equipId", "设备id");
        chkVar.put("status", "设备状态");
        ToolUtil.checkInputInfo("equipInfo", chkInfo, chkVar);
        String status = (String) chkInfo.get("status");
        if (!Constants.TABLE_RECORD_STATUS_NOMAL.equals(status)
                && !Constants.TABLE_RECORD_STATUS_INVALID.equals(status)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，设备状态传值不正确！"));
        }

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtil.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);
        Map<String, Object> retMap = equipManageService.setEquipStatus(mapIn);

        return new ResultModel<Object>(retMap);
    }

    // 设备列表查询
    @ResponseBody
    @RequestMapping("/queryEquip")
    public Map<String, Object> queryEquip(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in queryEquip----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 分页信息校验
        ToolUtil.checkPageInfo(mapIn);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtil.getSessionInfo(request);
        mapIn.put("tenantId", sessionMap.get("tenantId"));

        // 针对Datatable特殊处理
        String jsonStr = (String) mapIn.get(Constants.INF_INPUTPARAM_DATATABLE_FIELD);
        int draw = 0;
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonTable = (JSONObject) JSONObject.parse(jsonStr);
            draw = jsonTable.getIntValue("draw");
            mapIn.put("start", jsonTable.getIntValue("start"));
            mapIn.put("end", jsonTable.getIntValue("length"));
        }
        Map<String, Object> retMap = equipManageService.listEquipInfo(mapIn);
        // 针对Datatable特殊处理
        if (StringUtils.isNotBlank(jsonStr)) {
            retMap.put("draw", draw);
            retMap.put("recordsTotal", retMap.get("total"));
            retMap.put("recordsFiltered", retMap.get("total"));
        }

        return retMap;
    }

 // 查询当前上传组件是否存在
    @ResponseBody
    @RequestMapping("/imgClientApi/makeFlag")
    public ResultModel<Object> makeFlag(@RequestBody String arg, HttpServletRequest request) throws Exception {
        logger.warn("in makeFlag----arg==" + arg);

        // 基础校验
        ToolUtil.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        // 登录信息，从session中获取
        Map<String, Object> retMap = new HashMap<String, Object>();

        retMap = equipManageService.makeFlag(mapIn);

        return new ResultModel<Object>(retMap);
    }

}
